using System.IO;

namespace System.util.zlib
{
    /// <summary>
    /// Summary description for DeflaterOutputStream.
    /// </summary>
    public class ZInflaterInputStream : Stream
    {
        protected byte[] Buf = new byte[Bufsize];
        protected int FlushLevel = JZlib.Z_NO_FLUSH;
        protected Stream Inp;
        protected ZStream Z = new ZStream();
        private const int Bufsize = 4192;
        private readonly byte[] _buf1 = new byte[1];
        private bool _nomoreinput;

        public ZInflaterInputStream(Stream inp) : this(inp, false)
        {
        }

        public ZInflaterInputStream(Stream inp, bool nowrap)
        {
            Inp = inp;
            Z.InflateInit(nowrap);
            Z.NextIn = Buf;
            Z.NextInIndex = 0;
            Z.AvailIn = 0;
        }

        public override bool CanRead
        {
            get
            {
                // TODO:  Add DeflaterOutputStream.CanRead getter implementation
                return true;
            }
        }

        public override bool CanSeek
        {
            get
            {
                // TODO:  Add DeflaterOutputStream.CanSeek getter implementation
                return false;
            }
        }

        public override bool CanWrite
        {
            get
            {
                // TODO:  Add DeflaterOutputStream.CanWrite getter implementation
                return false;
            }
        }

        public override long Length
        {
            get
            {
                // TODO:  Add DeflaterOutputStream.Length getter implementation
                return 0;
            }
        }

        public override long Position
        {
            get
            {
                // TODO:  Add DeflaterOutputStream.Position getter implementation
                return 0;
            }
            set
            {
                // TODO:  Add DeflaterOutputStream.Position setter implementation
            }
        }

        public override void Flush()
        {
            Inp.Flush();
        }

        public override int Read(byte[] b, int off, int len)
        {
            if (len == 0)
                return (0);
            int err;
            Z.NextOut = b;
            Z.NextOutIndex = off;
            Z.AvailOut = len;
            do
            {
                if ((Z.AvailIn == 0) && (!_nomoreinput))
                { // if buffer is empty and more input is avaiable, refill it
                    Z.NextInIndex = 0;
                    Z.AvailIn = Inp.Read(Buf, 0, Bufsize);//(BUFSIZE<z.avail_out ? BUFSIZE : z.avail_out));
                    if (Z.AvailIn == 0)
                    {
                        Z.AvailIn = 0;
                        _nomoreinput = true;
                    }
                }
                err = Z.Inflate(FlushLevel);
                if (_nomoreinput && (err == JZlib.Z_BUF_ERROR))
                    return (-1);
                if (err != JZlib.Z_OK && err != JZlib.Z_STREAM_END)
                    throw new IOException("inflating: " + Z.Msg);
                if ((_nomoreinput || err == JZlib.Z_STREAM_END) && (Z.AvailOut == len))
                    return (0);
            }
            while (Z.AvailOut == len && err == JZlib.Z_OK);
            //System.err.print("("+(len-z.avail_out)+")");
            return (len - Z.AvailOut);
        }

        public override int ReadByte()
        {
            if (Read(_buf1, 0, 1) <= 0)
                return -1;
            return (_buf1[0] & 0xFF);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            // TODO:  Add DeflaterOutputStream.Seek implementation
            return 0;
        }

        public override void SetLength(long value)
        {
            // TODO:  Add DeflaterOutputStream.SetLength implementation

        }

        public override void Write(byte[] b, int off, int len)
        {
        }
        public override void WriteByte(byte b)
        {
        }
    }
}
